package cate.game.role.res.guild.po;

import cate.common.util.GameResult;
import cate.game.event.EventPublisher;
import cate.game.log.GameLoggerFactory;
import cate.game.role.FuncOpenChecker;
import cate.game.role.Role;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.practice.errorcode.EcResult;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.springframework.data.annotation.Transient;

import java.util.List;

@Data
@NoteClass(value = "公会子功能依赖信息")
@NoArgsConstructor
public class GuildFuncDependent implements EventPublisher {
    @Transient
    @JsonIgnore
    @Getter
    public transient GuildPO guild;

    @Transient
    @JsonIgnore
    private FuncOpenChecker funcOpenChecker;

    public static GuildFuncDependent of(GuildPO guild, Role role, int funcId) {
        GuildFuncDependent guildDependent = new GuildFuncDependent();
        guildDependent.setGuild(guild);
        guildDependent.setFuncOpenChecker(
                new FuncOpenChecker(role) {
                    @Override
                    public int getFuncId() {
                        return funcId;
                    }

                    @Override
                    public void onAfterOpen() {

                    }

                    @Override
                    public void checkOpen() {
                        if (role != null) {
                            GameResult<Void> openEc = role.getGame().table.common.funcEnable.isEnable(
                                    role.getBase().level,
                                    role.getBase().vip,
                                    role.getGame().getOpenDay(),
                                    0,
                                    getFuncId(),
                                    role.getFarm().adventure.getAdventurePassedNum()
                            );
                            if (!openEc.ok()) {
                                role.getGame().notice.message(role, openEc.message);
                                open = false;
                                return;
                            }
                        }

                        open = customizeCheckOpen();
                    }

                    @Override
                    public boolean customizeCheckOpen() {
                        EcResult<List<Object>> rFunc = GuildFuncUtil.checkFunkOpen(getFuncId(), guild, role);
                        if (!rFunc.ok()) {
                            List<Object> params = rFunc.data;
                            if (CollectionUtils.isNotEmpty(params)) {
                                int msgId = (int) params.get(0);
                                params.remove(0);
                                role.getGame().notice.message(role, msgId, rFunc.message, params);
                                return false;
                            }
                            role.getGame().notice.message(role, "功能未开启");
                            return false;
                        }
                        return true;
                    }
                }
        );
        return guildDependent;
    }

    public GuildFuncDependent setRole(Role role) {
        funcOpenChecker.initialize(role);
        return this;
    }

    public boolean checkOpen() {
        this.funcOpenChecker.checkOpen();
        return this.funcOpenChecker.isOpen();
    }

}
